<template>
  <el-dialog
    :visible.sync="visible"
    title="绑定店铺"
    width="300px"
    destroy-on-close
  >
    <el-form
      size="small"
      :model="formData"
      label-position="right"
      label-width="60px"
      :rules="rules"
      ref="formRef"
    >
      <el-row :offset="20">
        <el-col :span="24">
          <el-form-item prop="shopId" label="店铺">
            <el-select v-model.trim="formData.shopId" style="width: 100%">
              <el-option
                v-for="item in shopList"
                :key="item.id"
                :label="item.shopName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer_box">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { bindMallHandle, getListHandle } from '@/api/mallManage/index.js'
export default {
  name: 'AddOrEditDialog',
  dicts: ['MEALTYPE'],
  props: {
    type: String,
    submitSuccess: Function,
  },
  data() {
    return {
      visible: false,
      loading: false,
      formData: {},
      rules: {
        shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }],
      },
      shopList: [],
    }
  },
  methods: {
    handleOpen(row) {
      this.getShopList()
      this.visible = true
      this.formData = {
        id: row.id,
        shopId: row.shopId || undefined,
      }
    },
    async handleSubmit() {
      await this.$refs.formRef.validate()
      this.loading = true
      bindMallHandle(this.formData)
        .then((data) => {
          this.$notify.success(data)
          this.submitSuccess && this.submitSuccess()
          this.visible = false
        })
        .finally(() => {
          this.loading = false
        })
    },
    async getShopList() {
      let resData = await getListHandle({ page: 1, size: 100 })
      this.shopList = resData.records
    },
  },
}
</script>

<style lang="scss" scoped>
.footer_box {
  text-align: right;
}
</style>
